1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package com.sun.media.sound;
27
28 import java.io.BufferedInputStream;
29 import java.io.InputStream;
30 import java.io.File;
31 import java.io.FileInputStream;
32
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Properties;
37
38 import java.security.AccessController;
39 import java.security.PrivilegedAction;
40
41 import javax.sound.sampled.AudioPermission;
42
43 import sun.misc.Service;
44
45
46
47
48
49
50
51
52 class JSSecurityManager {
53
54
55
56 private JSSecurityManager() {
57 }
58
59
60
61
62
63
64
65 private static boolean hasSecurityManager() {
66 return (System.getSecurityManager() != null);
67 }
68
69
70 static void checkRecordPermission() throws SecurityException {
71 if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()");
72 SecurityManager sm = System.getSecurityManager();
73 if (sm != null) {
74 sm.checkPermission(new AudioPermission("record"));
75 }
76 }
77
78
79 static void loadLibrary(final String libName) {
80 try {
81 if (hasSecurityManager()) {
82 if(Printer.debug) Printer.debug("using security manager to load library");
83 PrivilegedAction action = new PrivilegedAction() {
84 public Object run() {
85 System.loadLibrary(libName);
86 return null;
87 }
88 };
89 AccessController.doPrivileged(action);
90 } else {
91 if(Printer.debug) Printer.debug("not using security manager to load library");
92 System.loadLibrary(libName);
93 }
94 if (Printer.debug) Printer.debug("loaded library " + libName);
95 } catch (UnsatisfiedLinkError e2) {
96 if (Printer.err)Printer.err("UnsatisfiedLinkError loading native library " + libName);
97 throw(e2);
98 }
99 }
100
101
102 static String getProperty(final String propertyName) {
103 String propertyValue;
104 if (hasSecurityManager()) {
105 if(Printer.debug) Printer.debug("using JDK 1.2 security to get property");
106 try{
107 PrivilegedAction action = new PrivilegedAction() {
108 public Object run() {
109 try {
110 return System.getProperty(propertyName);
111 } catch (Throwable t) {
112 return null;
113 }
114 }
115 };
116 propertyValue = (String) AccessController.doPrivileged(action);
117 } catch( Exception e ) {
118 if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties");
119 propertyValue = System.getProperty(propertyName);
120 }
121 } else {
122 if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties");
123 propertyValue = System.getProperty(propertyName);
124 }
125 return propertyValue;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140 static void loadProperties(final Properties properties,
141 final String filename) {
142 if(hasSecurityManager()) {
143 try {
144
145 PrivilegedAction action = new PrivilegedAction() {
146 public Object run() {
147 loadPropertiesImpl(properties, filename);
148 return null;
149 }
150 };
151 AccessController.doPrivileged(action);
152 if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security");
153 } catch (Exception e) {
154 if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security");
155
156 loadPropertiesImpl(properties, filename);
157 }
158 } else {
159
160 loadPropertiesImpl(properties, filename);
161 }
162 }
163
164
165 private static void loadPropertiesImpl(Properties properties,
166 String filename) {
167 if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()");
168 String fname = System.getProperty("java.home");
169 try {
170 if (fname == null) {
171 throw new Error("Can't find java.home ??");
172 }
173 File f = new File(fname, "lib");
174 f = new File(f, filename);
175 fname = f.getCanonicalPath();
176 InputStream in = new FileInputStream(fname);
177 BufferedInputStream bin = new BufferedInputStream(in);
178 try {
179 properties.load(bin);
180 } finally {
181 if (in != null) {
182 in.close();
183 }
184 }
185 } catch (Throwable t) {
186 if (Printer.trace) {
187 System.err.println("Could not load properties file \"" + fname + "\"");
188 t.printStackTrace();
189 }
190 }
191 if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed");
192 }
193
194
195 private static ThreadGroup getTopmostThreadGroup() {
196 ThreadGroup topmostThreadGroup;
197 if(hasSecurityManager()) {
198 try {
199
200 PrivilegedAction action = new PrivilegedAction() {
201 public Object run() {
202 try {
203 return getTopmostThreadGroupImpl();
204 } catch (Throwable t) {
205 return null;
206 }
207 }
208 };
209 topmostThreadGroup = (ThreadGroup) AccessController.doPrivileged(action);
210 if(Printer.debug)Printer.debug("Got topmost thread group with JDK 1.2 security");
211 } catch (Exception e) {
212 if(Printer.debug)Printer.debug("Exception getting topmost thread group with JDK 1.2 security");
213
214 topmostThreadGroup = getTopmostThreadGroupImpl();
215 }
216 } else {
217
218 topmostThreadGroup = getTopmostThreadGroupImpl();
219 }
220 return topmostThreadGroup;
221 }
222
223
224 private static ThreadGroup getTopmostThreadGroupImpl() {
225 if(Printer.trace)Printer.trace(">> JSSecurityManager: getTopmostThreadGroupImpl()");
226 ThreadGroup g = Thread.currentThread().getThreadGroup();
227 while ((g.getParent() != null) && (g.getParent().getParent() != null)) {
228 g = g.getParent();
229 }
230 if(Printer.trace)Printer.trace("<< JSSecurityManager: getTopmostThreadGroupImpl() completed");
231 return g;
232 }
233
234
235
236
237 static Thread createThread(final Runnable runnable,
238 final String threadName,
239 final boolean isDaemon, final int priority,
240 final boolean doStart) {
241 Thread thread = null;
242 if(hasSecurityManager()) {
243 PrivilegedAction action = new PrivilegedAction() {
244 public Object run() {
245 try {
246 return createThreadImpl(runnable, threadName,
247 isDaemon, priority,
248 doStart);
249 } catch (Throwable t) {
250 return null;
251 }
252 }
253 };
254 thread = (Thread) AccessController.doPrivileged(action);
255 if(Printer.debug) Printer.debug("created thread with JDK 1.2 security");
256 } else {
257 if(Printer.debug)Printer.debug("not using JDK 1.2 security");
258 thread = createThreadImpl(runnable, threadName, isDaemon, priority,
259 doStart);
260 }
261 return thread;
262 }
263
264
265 private static Thread createThreadImpl(Runnable runnable,
266 String threadName,
267 boolean isDaemon, int priority,
268 boolean doStart) {
269 ThreadGroup threadGroup = getTopmostThreadGroupImpl();
270 Thread thread = new Thread(threadGroup, runnable);
271 if (threadName != null) {
272 thread.setName(threadName);
273 }
274 thread.setDaemon(isDaemon);
275 if (priority >= 0) {
276 thread.setPriority(priority);
277 }
278 if (doStart) {
279 thread.start();
280 }
281 return thread;
282 }
283
284
285 static List getProviders(final Class providerClass) {
286 List p = new ArrayList();
287
288
289 final Iterator ps = Service.providers(providerClass);
290
291
292
293 PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
294 public Boolean run() {
295 return ps.hasNext();
296 }
297 };
298
299 while (AccessController.doPrivileged(hasNextAction)) {
300 try {
301
302
303
304 Object provider = ps.next();
305 if (providerClass.isInstance(provider)) {
306
307
308
309
310 p.add(0, provider);
311 }
312 } catch (Throwable t) {
313
314 if (Printer.err) t.printStackTrace();
315 }
316 }
317 return p;
318 }
319 }